package com.yz.design.lambda;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.*;

/**
 * 口罩
 */
public class Mask {
    public Mask() {
    }

    public Mask(String brand, String type, double price) {
        this.brand = brand;
        this.type = type;
        this.price = price;
    }

    /**
     * 品牌
     */
    private String brand;
    /**
     * 类型
     */
    private String type;
    /**
     * 价格
     */
    private double price;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Mask{" +
                "brand='" + brand + '\'' +
                ", type='" + type + '\'' +
                ", price=" + price +
                '}';
    }

    public static void main(String[] args) {
//        comparChainTest();
//        supplierTest();
//        consumerTest();
//        predicateTest();
        functionTest();
//        biFunctionTest();
    }

    /**
     * Comparator 比较器链使用
     *
     * @author: 广长
     * @date: 2020/11/13
     * @return: void
     */
    private static void comparChainTest() {
        List<Mask> maskList = new ArrayList<>();
        maskList.add(new Mask("3M", "KN95", 17.8));
        maskList.add(new Mask("3M", "KN95", 18.8));
        maskList.add(new Mask("Honeywell", "KN95", 18.8));
        maskList.add(new Mask("Honeywell", "KN95", 12.8));
        maskList.add(new Mask("3M", "FFP2", 19.8));
        maskList.add(new Mask("3M", "FFP2", 21.8));
        maskList.add(new Mask("Honeywell", "N95", 19.5));
        maskList.add(new Mask("Honeywell", "N95", 24.5));
        //先按品牌排序，再根据类型排序，在根据价格倒序排序
        maskList.sort(Comparator.comparing(Mask::getBrand).thenComparing(Mask::getType).thenComparingDouble(Mask::getPrice).reversed());
        for (Mask mask : maskList) {
            System.out.println(mask);
        }
    }

    /**
     * Supplier函数式接口使用
     * 供应商
     *
     * @author: 广长
     * @date: 2020/11/13
     * @return: void
     */
    private static void supplierTest() {
        Supplier<Mask> supplier = () -> new Mask("3M", "KN95", 17.8);
        Mask mask = supplier.get();
        System.out.println(mask);
    }

    /**
     * Consumer函数式接口使用
     * 消费者
     *
     * @author: 广长
     * @date: 2020/11/13
     * @return: void
     */
    private static void consumerTest() {
        Supplier<Mask> supplier = () -> new Mask("3M", "KN95", 17.8);
        Consumer<Mask> consumer = System.out::println;
        consumer.accept(supplier.get());
    }

    /**
     * Predicate函数式接口使用
     * 判断是与否
     *
     * @author: 广长
     * @date: 2020/11/13
     * @return: void
     */
    private static void predicateTest() {
        Supplier<Mask> supplier = () -> new Mask("3M", "KN95", 17.8);
        Predicate<Mask> predicate = make -> "3M".equals(make.getBrand());
        Predicate<Mask> predicate2 = make -> "KN95".equals(make.getType());
        System.out.println("是否是3M品牌的KN95类型：" + predicate.and(predicate2).test(supplier.get()));
    }

    /**
     * Function函数式接口使用
     * 对实例进行处理转换的接口
     *
     * @author: 广长
     * @date: 2020/11/13
     * @return: void
     */
    private static void functionTest() {
        Supplier<Mask> supplier = () -> new Mask("3M", "KN95", 17.8);
        Function<Mask, Mask> function = mask -> {
            System.out.println(mask);
            mask.setBrand("Honeywell");
            mask.setType("KN95");
            mask.setPrice(1);
            System.out.println(mask);
            return mask;
        };
        Mask apply = function.apply(supplier.get());
        System.out.println(apply);


    }


    /**
     * BiFunction函数式接口使用
     *
     * @author: 广长
     * @date: 2020/11/13
     * @return: void
     */
    private static void biFunctionTest() {
        Supplier<Mask> supplier = () -> new Mask("3M", "KN95", 17.8);
        Mask mask = supplier.get();
        BiFunction<String, String, Mask> biFunction = (brand, type) -> new Mask(brand, type, 100.2);
        Mask mask2 = biFunction.apply(mask.getBrand(), mask.getType());
        System.out.println(mask2.toString());
    }


}